import { Module } from "vuex";
// 维度数据本地缓存实现方案，通过维度类型编码为key对应维度的值，缓存在前端内存
import http from "@/utils/servers/index.js";
import { getDimensionByCode } from "@/utils/sys.js";
const types = {
  SET_DIMENSION: "set_dimension",
};
// state
const state = {
// 维度对象列表
    list: {}
};

// getters
const getters = {

    getDimensionByCode: (state) => async (code) => {
        if(state.list[code]){
            return state.list[code];
        } else {
            // 查询维度数据
           let res = await http.get(`business/dimension/getDimensionByCode?code=${code}`);
           if(res.status === 'success') {
                state.list[code] = res.data;
                return res.data;
           }
           return []
          
        }
    }
};

// mutations
const mutations = {
  [types.SET_DIMENSION ](state, key, value) {
    state[key] = value;
  }
};

// actions
const actions = {
    setDimensionByCode({ commit }, key, value) {
        commit(types.SET_DIMENSION, key, value);
    },
    // 初始化一些基础维度
    initDimension({ commit, getters }) {
      let arr = [
          'DIM_TYPE_NODE_TYPE', // 节点类型
          'DIM_TYPE_STATE', // 状态
    ];
    arr.forEach((item) => {
      getters.getDimensionByCode(item);
    });

    },
};

const dimension = {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};

export default dimension;
